//
//  ControlTemplateRadioHouseViewController.m
//  ExcellentHome
//
//  Created by Chiang on 2017/11/21.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "ControlTemplateRadioHouseViewController.h"
#import "ChooseRentalHouseViewDelegate.h"
#import "SEGChooseRentalHouseViewController.h"

@interface ControlTemplateRadioHouseViewController ()<ChooseRentalHouseViewDelegate>
{
    UILabel* valueLabel;
}
@property (nonatomic, retain) NSString* defaultValue;
@property (nonatomic, copy) NSString *selectHouseID; // 选择的房屋ID

@end

@implementation ControlTemplateRadioHouseViewController

static ControlTemplateRadioHouseViewController* sharedManagerRadioHouse = nil;

+ (ControlTemplateRadioHouseViewController*)sharedManagerRadioHouse {
    sharedManagerRadioHouse = [[self alloc] init];
    return sharedManagerRadioHouse;
}

- (void)initVar
{
    _valueStr = @"";
    _cnValueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    _defaultValue = [_params valueForKey:@"attrValue"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initVar];
    self.isNeedAdpterHotspot = YES;
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel extendAttributedString:[_params valueForKey:@"title"] lineSpace:LINESPACE_6 font:kFont_16];
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    //默认地址
    NSString *defaultStr = [SEGUtils dictionaryWithJsonString:_defaultValue][@"value"];
    //是否必填 //是否可读 0-可不写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) {

        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
        [valueLabel extendAttributedString:CHECKVALUE(defaultStr) lineSpace:LINESPACE_6 font:kFont_16];
        [valueLabel setTextAlignment:NSTextAlignmentLeft];
        [valueLabel setFont:kFont_16];
        [valueLabel setTextColor:kColor_1];
        valueLabel.numberOfLines = 0;
        [_bgMainView addSubview:valueLabel];
    }
    else {
        CustomButtonView* abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 50)];
        abtn.tempData = _params;
        [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:abtn];
        
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
        UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(_bgMainView.bounds) - image.size.width - HEADSPACE, (CGRectGetHeight(_bgMainView.bounds) - image.size.height) / 2, image.size.width, image.size.height)];
        [iconImageView setImage:image];
        [_bgMainView addSubview:iconImageView];
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_bgMainView.mas_centerY);
            make.right.equalTo(_bgMainView.mas_right).offset(-HEADSPACE);
        }];
        
        valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT -image.size.width, TEMPLATE_HEIGHT)];
        [valueLabel setTextAlignment:NSTextAlignmentLeft];
        valueLabel.tag = 999;
        valueLabel.numberOfLines = 0;
        [valueLabel setText:[_params valueForKey:@"markedWords"]];
        [valueLabel setFont:kFont_16];
        [valueLabel setTextColor:kColor_C];
        [_bgMainView addSubview:valueLabel];
        
        if (defaultStr.length > 0) {
            [valueLabel extendAttributedString:CHECKVALUE(defaultStr) lineSpace:LINESPACE_6 font:kFont_16];
            [valueLabel setTextColor:kColor_1];
            _valueStr = [_params valueForKey:@"attrValue"];
            _optional = @"0";
        }
       
    }
    
    [self refreshUI];
}

- (void)onBtnAction:(CustomButtonView*)sender
{


    NSString *selectHouseID = self.selectHouseID;
    if (!selectHouseID || selectHouseID.length == 0) {
        selectHouseID = @"-1";
    }

    SEGChooseRentalHouseViewController *vc = [SEGChooseRentalHouseViewController new];
    vc.chooseRentalHouseDelegate = self;
    vc.modelKey = self.param_id;
    vc.isServiceAppointment = YES;
    vc.selectHouseID = selectHouseID;
    vc.hidesBottomBarWhenPushed = YES;
    [self.masterVC.navigationController pushViewController:vc animated:YES];
}

#pragma mark -ChooseRentalHouseViewDelegate

- (void)selectHouseType:(NSString*)nameSt withHouseId:(NSString*)houseID
                withKey:(NSString*)key
{
    self.selectHouseID = houseID;
    NSDictionary *attrValue = @{@"id" : CHECKVALUE(houseID), @"value" : CHECKVALUE(nameSt)};
    [valueLabel extendAttributedString:CHECKVALUE(nameSt) lineSpace:LINESPACE_6 font:kFont_16];
    [valueLabel setTextColor:kColor_1];
    _valueStr = [attrValue jsonPrettyStringEncoded];
    
    _optional = @"0";
    [self refreshUI];
}

-(void)refreshUI {
    CGSize size = [_cnValueStr getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:CGRectGetWidth(valueLabel.bounds)];
    size.height = size.height > 24 ? size.height : 24;
    valueLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 0, CGRectGetWidth(valueLabel.bounds), TEMPLATE_HEIGHT -24 + size.height);
    _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(valueLabel.bounds));
    UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
    lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
    if (self.fundelegate) {
        [self.fundelegate onFeeRefeshUI];
    }
}

@end
